﻿--[[
   @brief    friend
   @author   rickonzhang
   @version  1.0
   @date     2015-07-21
--]]
--[[
json_param["friend_id"] = it->szId;
json_param["name"] = it->szName;
json_param["remark"] = it->szRemark;                                 /*   介绍 */
json_param["url"] = it->szFace_url;
json_param["sex"] = it->dwGender;                                   /*   性别: 0男,1女,其他未知 */
json_param["level"] = it->dwLevel;                                  /*   介绍 */
json_param["exp"] = it->dwExp;                                      /*   介绍 */
json_param["reg_time"] = it->dwReg_time;                                /*   介绍 */
json_param["last_login_time"] = it->dwLast_login_time;                          /*   介绍 */
]]

friend_guide_banner = {
    
};

function friend_guide_banner:new(root_id, friend_info)
    local obj = {root_id_ = root_id, friend_info_ = friend_info};
    setmetatable(obj, self);
    self.__index = self;
    obj:init();
    return obj;
end

function friend_guide_banner:init()
    local image_str = string.format("ONEIMAGE:social_network\\friend-head-small.png|%s;Stretch;0;", self.friend_info_["url"]);
    self:setproperty("Button_Head_Image", "Background", image_str);

    local name = self.friend_info_["name"];
    self:setproperty("Button_Friend_Name", "Caption", name);

    local remark = self.friend_info_["remark"];
    self:setproperty("Static_Friend_Info", "Caption", remark);
end

function friend_guide_banner:setproperty(caption, property, value)
    local ui_str, str_holder = U8toCUIStr(caption);
    local uilib = ui_utility.uilib();
    local c_id = uilib:GetControlIDbyPath(ui_str, self.root_id_);
    return ui_utility.setproperty_by_id(c_id, property, value);
end

function friend_guide_banner:get_friend_info_str()
    local ext_info = {};
    ext_info["type"] = 1 --关系:1 qq好友 2 游戏好友, 3-未知, 如果有最近游戏字段， 填写游戏id ext_info["game_id"] =， 没有则不填写
    local from = {};
    if login_model.im_login_infomation then
        from = social_network_control.get_my_remark_info();
    end
    ext_info["f"] = from;
    return base_utility.table_to_json(ext_info);
end

function friend_guide_banner:get_friend_id()
    return self.friend_info_["friend_id"];
end

--切换添加好友按钮到已添加状态
function friend_guide_banner:change_add_btn_to_hid()
    self:setproperty("TinyStatic_AlreadyFlag", "Visible", "true");
    self:setproperty("Button_AddFriend", "Visible", "false");
end
--------------------------------------------------------------------------
--------------------------------------------------------------------------

friend_guide = {
    friend_list_ctrl_ = nil;
    uilib_ = nil;
    first_open = false;
    sub_id_to_item = {};
    root_id_ = 0;
};

function friend_guide:log(msg, core_msg)
    base_utility.log(string.format("[friend_guide] %s", msg), core_msg);
end

function friend_guide:init(obj_id)
    local user_obj_ptr = ui_utility.get_ctrl_ptr_by_id(obj_id);
    self.root_id_ = obj_id;
    self.uilib_ = ui_utility.uilib();
    local ui_str, str_holder = U8toCUIStr("TinyContainer_FriendRecommend\\Container_1\\Button_3");
    if nil ~= user_obj_ptr and nil ~= self.uilib_ then
        local find_game_friend_id = self.uilib_:GetControlIDbyPath(ui_str, user_obj_ptr:GetControlID());
        self.game_friend_btn_ctrl_id_ = find_game_friend_id;
    end
    
    --[[
    self.uilib_ = ui_utility.uilib();
    local ui_str, str_holder = U8toCUIStr("TinyContainer_FriendRecommend\\DraggableList_PushFriend");
    local list_id = self.uilib_:GetControlIDbyPath(ui_str, user_obj_ptr:GetControlID());
    if list_id then
        base_utility.log("list_id:"..tostring(list_id));
    end
    self.friend_list_ctrl_ = ui_utility.get_ctrl_ptr_by_id(list_id);
    if self.friend_list_ctrl_ == nil then
        base_utility.log("self.friend_list_ctrl nill");
    end]]
end

function friend_guide:show(show)
    if self.root_id_ > 0 then
        if show == TRUE then
            ui_utility.setproperty_by_id(self.root_id_, "Visible", "true");
            if base_utility.is_type_of_oversea_ver() then
                ui_utility.setproperty_by_id(self.game_friend_btn_ctrl_id_, "Visible", "false");
            end
            --[[
            if self.first_open == false then 
                self:get_recommend_friend();
                self.first_open = true;
            end
            --]]
        else
           ui_utility.setproperty_by_id(self.root_id_, "Visible", "false");
        end
    end
end

function friend_guide:get_recommend_friend()
    friend_list_control.get_recommend_friend();
end

function friend_guide:get_recommend_friend_rsp(friend_list)
    -- 先填写一个
    --[[
    json_param["friend_id"] = it->szId;
    json_param["name"] = it->szName;
    json_param["remark"] = it->szRemark;
    json_param["url"] = it->szFace_url;
    json_param["sex"] = it->dwGender;                                   /*   性别: 0男,1女,其他未知 */
    json_param["level"] = it->dwLevel;                                  /*   介绍 */
    json_param["exp"] = it->dwExp;                                      /*   介绍 */
    json_param["reg_time"] = it->dwReg_time;                                /*   介绍 */
    json_param["last_login_time"] = it->dwLast_login_time;                          /*   介绍 */

    arr_param["friends"].append(json_param);

    friend_list = {};
    friend_list["result"] = 0;
    friend_list["friends"] = {}
    local one_friend = {};
    one_friend["friend_id"] = "17788812";
    one_friend["name"] = "好好学习";
    one_friend["url"] = "http://10.143.150.139/data1/80/tgp_social/test/6.jpg";
    one_friend["remark"] = "小妹，一起来玩游戏";
    table.insert(friend_list["friends"], one_friend);

    one_friend = {};
    one_friend["friend_id"] = "273086603";
    one_friend["name"] = "测试同学(3621..)";
    one_friend["url"] = "http://10.143.150.139/data1/80/tgp_social/test/6.jpg";
    one_friend["remark"] = "小妹，一起来玩游戏";
    table.insert(friend_list["friends"], one_friend);

     one_friend = {};
    one_friend["friend_id"] = "1252274634";
    one_friend["name"] = "测试同学(1252..)";
    one_friend["url"] = "http://10.143.150.139/data1/80/tgp_social/test/6.jpg";
    one_friend["remark"] = "小妹，一起来玩游戏";
    table.insert(friend_list["friends"], one_friend);

    ---------------------------------------------------------------
]]
    self:remove_all_friend();
    self.sub_id_to_item = {};
    if friend_list["result"] == 0 and friend_list["friends"] then
        for index, friend in ipairs(friend_list["friends"]) do
            self:insert_one_friend(index-1, friend);
        end
    end

    local user_obj_ptr = ui_utility.get_ctrl_ptr_by_id(self.root_id_);
    local ui_str, str_holder = U8toCUIStr("TinyContainer_FriendRecommend\\TinyStatic_1");
    local recommend_id = self.uilib_:GetControlIDbyPath(ui_str, user_obj_ptr:GetControlID());

    ui_str, str_holder = U8toCUIStr("TinyContainer_FriendRecommend\\TinyContainer_1");
    local line_id = self.uilib_:GetControlIDbyPath(ui_str, user_obj_ptr:GetControlID());    

    ui_str, str_holder = U8toCUIStr("TinyContainer_FriendRecommend\\Button_1");
    local button_id = self.uilib_:GetControlIDbyPath(ui_str, user_obj_ptr:GetControlID());  

    if friend_list["friends"] ~= nil and #friend_list["friends"] > 0 then
       ui_utility.setproperty_by_id(recommend_id, 'Visible', 'true');
       ui_utility.setproperty_by_id(line_id, 'Visible', 'true');
       ui_utility.setproperty_by_id(button_id, 'Visible', 'true');
    else
       ui_utility.setproperty_by_id(recommend_id, 'Visible', 'false');
       ui_utility.setproperty_by_id(line_id, 'Visible', 'false');
       ui_utility.setproperty_by_id(button_id, 'Visible', 'false');
    end
end

function friend_guide:insert_one_friend(index, friend)
    local ui_str, str_holder = U8toCUIStr("social\\friend_banner_for_recommend.tpf");
    local sub_id = self.friend_list_ctrl_:InsertItem(index, ui_str)
    self:log(string.format("%d", sub_id));
    local one_banner = friend_guide_banner:new(sub_id, friend);
    self.sub_id_to_item[sub_id] = one_banner;
end

function friend_guide:remove_all_friend()
    while self.friend_list_ctrl_:GetItemCount() > 0 do
        if self.friend_list_ctrl_:RemoveItem(0) == false then
            base_utility.log("delete sub item error");
        end
    end
end

function friend_guide:click_on_add_friend_btn(uilib, ctrl_id)
    base_utility.log("[friend_guide:click_on_add_friend_btn]");
    local p_id = self:get_parent_id(ctrl_id);
    local item = self.sub_id_to_item[p_id];
    if item then
        local friend_id = item:get_friend_id();
        local ext_info = item:get_friend_info_str();
        self:add_friend(friend_id, ext_info);
        item:change_add_btn_to_hid();
    end
end

function friend_guide:add_friend(friend_id, reason)
    friend_list_control.add_friend(friend_id, reason);
end

function friend_guide:add_friend_rsp(json_param)
    --[[
    json_param["result"];
    json_param["friend_id"];
    ]]
    -- do nothing right now
end

function friend_guide:click_on_change_recommend_friend(uilib, ctrl_id)
    self:get_recommend_friend();
end

function friend_guide:click_on_find_qq_friend(uilib, ctrl_id)
    comm_center.call_service("i_service_tqos_guide_find_qq_friend_click", {});
    comm_center.send_broadcast("i_service_friend_guide_click_on_find_qq_friend", {});
end

function friend_guide:click_on_find_game_friend(uilib, ctrl_id)
    comm_center.call_service("i_service_tqos_guide_find_game_friend_click", {});
    comm_center.send_broadcast("i_service_friend_guide_click_on_find_game_friend", {});
end

function friend_guide:click_on_find_new_friend(uilib, ctrl_id)
    comm_center.call_service("i_service_tqos_guide_find_qq_friend_click", {});-- 复用上报
    comm_center.send_broadcast("i_service_friend_guide_click_on_find_new_friend", {});
end

function friend_guide:click_on_image_or_name(uilib, ctrl_id)
    local p_id = self:get_parent_id(ctrl_id);
    local item = self.sub_id_to_item[p_id];
    if item then
        local param = {friend_id = item:get_friend_id()}
        event_center.send_event(group_event.SHOW_PERSONAL_CENTER,base_utility.table_to_json(param));
    end
end

function friend_guide:get_parent_id(sub_id)
    local s_ptr =ui_utility.get_ctrl_ptr_by_id(sub_id);
    if s_ptr then
        local p_ptr = s_ptr:GetParent();
        return p_ptr:GetControlID();
    end
end
